﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace TamGiac
{
    public partial class frm_TamGiac : Form
    {
        public frm_TamGiac()
        {
            InitializeComponent();
        }

        private void r_Diem_CheckedChanged(object sender, EventArgs e)
        {
            if (r_Diem.Checked == true)
            {
                gb1.Text = "Toạ độ đỉnh 1";
                gb2.Text = "Toạ độ đỉnh 2";
                gb3.Text = "Toạ độ đỉnh 3";

                tbc1.Enabled = false;
                tbc2.Enabled = false;
                tbc3.Enabled = false;

                tbX1.Enabled = true;
                tbX2.Enabled = true;
                tbX3.Enabled = true;
                tbY1.Enabled = true;
                tbY2.Enabled = true;
                tbY3.Enabled = true;
            }
        }

        private void r_Canh_CheckedChanged(object sender, EventArgs e)
        {
            if (r_Canh.Checked == true)
            {
                gb1.Text = "Độ dài cạnh 1";
                gb2.Text = "Độ dài cạnh 2";
                gb3.Text = "Độ dài cạnh 3";

                tbc1.Enabled = true;
                tbc2.Enabled = true;
                tbc3.Enabled = true;

                tbc1.Clear();
                tbc2.Clear();
                tbc3.Clear();

                tbX1.Enabled = false;
                tbX2.Enabled = false;
                tbX3.Enabled = false;
                tbY1.Enabled = false;
                tbY2.Enabled = false;
                tbY3.Enabled = false;

                tbX1.Clear();
                tbX2.Clear();
                tbX3.Clear();
                tbY1.Clear();
                tbY2.Clear();
                tbY3.Clear();
            }
        }

        private void bt_Thucthi_Click(object sender, EventArgs e)
        {
            _0912218_LopTamGiac xuLyTG = new _0912218_LopTamGiac();
            TamGiac tg;
            Graphics graphics = ptb_hinhve.CreateGraphics();
                        
            if (r_Canh.Checked == true)
            {   
                tg = new TamGiac(Convert.ToDouble(tbc1.Text), Convert.ToDouble(tbc2.Text), Convert.ToDouble(tbc3.Text));

                if (xuLyTG.KiemTraTamGiac(tg))
                {
                    graphics.Clear(Color.White);

                    List<Point> mangDiem = new List<Point>();

                    mangDiem = xuLyTG.taoDiem(tg);

                    List<int> lst = new List<int>();

                    lst.AddRange(new int[] { mangDiem[0].X, mangDiem[0].Y, mangDiem[1].X, mangDiem[1].Y, mangDiem[2].X, mangDiem[2].Y });

                    int soNhan = xuLyTG.soNhan(lst);

                    Point[] pts = new Point[]{
                        new Point(mangDiem[0].X * soNhan, mangDiem[0].Y * soNhan),
                        new Point(mangDiem[1].X * soNhan, mangDiem[1].Y * soNhan),
                        new Point(mangDiem[2].X * soNhan, mangDiem[2].Y * soNhan)
                        };

                    graphics.DrawPolygon(Pens.Red, pts);            

                }
            } else
            {
                graphics.Clear(Color.White);
                                
                Diem d01 = new Diem(Convert.ToDouble(tbX1.Text), Convert.ToDouble(tbY1.Text));
                Diem d02 = new Diem(Convert.ToDouble(tbX2.Text), Convert.ToDouble(tbY2.Text));
                Diem d03 = new Diem(Convert.ToDouble(tbX3.Text), Convert.ToDouble(tbY3.Text));

                tg = new TamGiac(d01, d02, d03);

                List<int> lst = new List<int>();

                lst.AddRange(new int[] { Convert.ToInt32(tg.Diem1.X), Convert.ToInt32(tg.Diem1.Y), 
                                        Convert.ToInt32(tg.Diem2.X), Convert.ToInt32(tg.Diem2.Y), 
                                        Convert.ToInt32(tg.Diem3.X), Convert.ToInt32(tg.Diem3.Y) });

                int soNhan = xuLyTG.soNhan(lst);

                Point[] pts = new Point[]{
                    new Point(Convert.ToInt32(tbX1.Text) * soNhan, Convert.ToInt32(tbY1.Text) * soNhan),
                    new Point(Convert.ToInt32(tbX2.Text) * soNhan, Convert.ToInt32(tbY2.Text) * soNhan),
                    new Point(Convert.ToInt32(tbX3.Text) * soNhan, Convert.ToInt32(tbY3.Text) * soNhan)                    
                    };

                graphics.DrawPolygon(Pens.Red, pts);            
            }

            if (xuLyTG.KiemTraTamGiac(tg))
            {
                switch (xuLyTG.XacDinhLoaiTamGiac(tg))
                {
                    case 0:
                        lb_Ketqua.Text = "Tam giác thường";
                        break;

                    case 1:
                        lb_Ketqua.Text = "Tam giác cân";
                        break;

                    case 2:
                        lb_Ketqua.Text = "Tam giác vuông";
                        break;

                    case 3:
                        lb_Ketqua.Text = "Tam giác đều";
                        break;
                }
            } else
            {
                lb_Ketqua.Text = "Không là tam giác";
            }
        }

        private void tbX1_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            string NegativeString = Thread.CurrentThread.CurrentCulture.NumberFormat.NegativeSign;
            char decimalChar = Convert.ToChar(decimalString);
            char NegativeChar = Convert.ToChar(NegativeString);
            if ((Char.IsDigit(e.KeyChar)) || (Char.IsControl(e.KeyChar))) 
            {                   
                
            } else if ((e.KeyChar == decimalChar && tbX1.Text.IndexOf(decimalString) == -1) ||
                        (e.KeyChar == NegativeChar && tbX1.Text.IndexOf(NegativeString) == -1))
            {                  
            } else
            {
                e.Handled = true;
            }
        }

        private void tbY1_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            string NegativeString = Thread.CurrentThread.CurrentCulture.NumberFormat.NegativeSign;
            char decimalChar = Convert.ToChar(decimalString);
            char NegativeChar = Convert.ToChar(NegativeString);
            if ((Char.IsDigit(e.KeyChar)) || (Char.IsControl(e.KeyChar)))
            {

            } else if ((e.KeyChar == decimalChar && tbY1.Text.IndexOf(decimalString) == -1) ||
                        (e.KeyChar == NegativeChar && tbY1.Text.IndexOf(NegativeString) == -1))
            {
            } else
            {
                e.Handled = true;
            }
        }

        private void tbc1_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            string NegativeString = Thread.CurrentThread.CurrentCulture.NumberFormat.NegativeSign;
            char decimalChar = Convert.ToChar(decimalString);
            char NegativeChar = Convert.ToChar(NegativeString);
            if ((Char.IsDigit(e.KeyChar)) || (Char.IsControl(e.KeyChar)))
            {

            } else if ((e.KeyChar == decimalChar && tbc1.Text.IndexOf(decimalString) == -1) ||
                        (e.KeyChar == NegativeChar && tbc1.Text.IndexOf(NegativeString) == -1))
            {
            } else
            {
                e.Handled = true;
            }
        }

        private void tbX2_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            string NegativeString = Thread.CurrentThread.CurrentCulture.NumberFormat.NegativeSign;
            char decimalChar = Convert.ToChar(decimalString);
            char NegativeChar = Convert.ToChar(NegativeString);
            if ((Char.IsDigit(e.KeyChar)) || (Char.IsControl(e.KeyChar)))
            {

            } else if ((e.KeyChar == decimalChar && tbX2.Text.IndexOf(decimalString) == -1) ||
                        (e.KeyChar == NegativeChar && tbX2.Text.IndexOf(NegativeString) == -1))
            {
            } else
            {
                e.Handled = true;
            }
        }

        private void tbY2_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            string NegativeString = Thread.CurrentThread.CurrentCulture.NumberFormat.NegativeSign;
            char decimalChar = Convert.ToChar(decimalString);
            char NegativeChar = Convert.ToChar(NegativeString);
            if ((Char.IsDigit(e.KeyChar)) || (Char.IsControl(e.KeyChar)))
            {

            } else if ((e.KeyChar == decimalChar && tbY2.Text.IndexOf(decimalString) == -1) ||
                        (e.KeyChar == NegativeChar && tbY2.Text.IndexOf(NegativeString) == -1))
            {
            } else
            {
                e.Handled = true;
            }
        }

        private void tbc2_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            string NegativeString = Thread.CurrentThread.CurrentCulture.NumberFormat.NegativeSign;
            char decimalChar = Convert.ToChar(decimalString);
            char NegativeChar = Convert.ToChar(NegativeString);
            if ((Char.IsDigit(e.KeyChar)) || (Char.IsControl(e.KeyChar)))
            {

            } else if ((e.KeyChar == decimalChar && tbc2.Text.IndexOf(decimalString) == -1) ||
                        (e.KeyChar == NegativeChar && tbc2.Text.IndexOf(NegativeString) == -1))
            {
            } else
            {
                e.Handled = true;
            }
        }

        private void tbX3_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            string NegativeString = Thread.CurrentThread.CurrentCulture.NumberFormat.NegativeSign;
            char decimalChar = Convert.ToChar(decimalString);
            char NegativeChar = Convert.ToChar(NegativeString);
            if ((Char.IsDigit(e.KeyChar)) || (Char.IsControl(e.KeyChar)))
            {

            } else if ((e.KeyChar == decimalChar && tbX3.Text.IndexOf(decimalString) == -1) ||
                        (e.KeyChar == NegativeChar && tbX3.Text.IndexOf(NegativeString) == -1))
            {
            } else
            {
                e.Handled = true;
            }
        }

        private void tbY3_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            string NegativeString = Thread.CurrentThread.CurrentCulture.NumberFormat.NegativeSign;
            char decimalChar = Convert.ToChar(decimalString);
            char NegativeChar = Convert.ToChar(NegativeString);
            if ((Char.IsDigit(e.KeyChar)) || (Char.IsControl(e.KeyChar)))
            {

            } else if ((e.KeyChar == decimalChar && tbY3.Text.IndexOf(decimalString) == -1) ||
                        (e.KeyChar == NegativeChar && tbY3.Text.IndexOf(NegativeString) == -1))
            {
            } else
            {
                e.Handled = true;
            }
        }

        private void tbc3_KeyPress(object sender, KeyPressEventArgs e)
        {
            string decimalString = Thread.CurrentThread.CurrentCulture.NumberFormat.CurrencyDecimalSeparator;
            string NegativeString = Thread.CurrentThread.CurrentCulture.NumberFormat.NegativeSign;
            char decimalChar = Convert.ToChar(decimalString);
            char NegativeChar = Convert.ToChar(NegativeString);
            if ((Char.IsDigit(e.KeyChar)) || (Char.IsControl(e.KeyChar)))
            {

            } else if ((e.KeyChar == decimalChar && tbc3.Text.IndexOf(decimalString) == -1) ||
                        (e.KeyChar == NegativeChar && tbc3.Text.IndexOf(NegativeString) == -1))
            {
            } else
            {
                e.Handled = true;
            }
        }
    }
}
